<?php 
namespace app\index\controller;
use think\Controller;


/*
*     分区
*
 */
class Modular extends Controller
{
	public function index($id)
	{
		$title = db('cate')->where('id',$id)->find()['catename'];
		$data = db('cate')->where('pid',$id)->select();
        foreach ($data as &$val) {
           $val['note'] = db('note')->where(['cateid'=>$val['id'],'status'=>'0'])->field('id,uid,title,time,respond,scan')->order('id','desc')->limit(1)->find();
           if ($val['note'] != '') {
                $val['note']['name'] = db('information')->where('userid',$val['note']['uid'])->find()['name'];
                $val['note']['time'] = date('Y-m-d H:i',$val['note']['time']);
            }
        }
        // var_dump($data);die;
		$this->assign('title',$title);
		$this->assign('data',$data);
    	$news = db('news')->select();
		$this->assign('news', $news);

        // 所有版主查询
        $banzhu = db('information')->field('userid,name')->where('type',2)->select();
        $this->assign("banzhu",$banzhu);

        // 所有帖子查询
        $notenum = db('note')->where('status','0')->count('*');
        $this->assign("notenum",$notenum);

        // 所有用户查询
        $usernum = db('user')->count('*');
        $this->assign("usernum",$usernum);
        
		  //初始化curl
        $curl = curl_init();
        // var_dump($curl);

        // 设置APIKEY   URL
        // $apikey = "";//聚合
        $apikey = "1e06dfde5f1247ed910b88697f994206";//易源
        $apiid = "32428";//易源
        $word = urlencode('lol');//%E8%8B%B1%E9%9B%84%E8%81%94%E7%9B%9F
        // echo 'http://v.juhe.cn/toutiao/index?key='.$apikey.'&num=10&keyword='.$word;
        // die;

        // http://route.showapi.com/109-35
        //CURL配置
        curl_setopt($curl, CURLOPT_URL, 'http://route.showapi.com/109-35?showapi_appid='.$apiid.'&showapi_sign='.$apikey.'&title=lol&maxResult=90');

        // curl_setopt($curl, CURLOPT_URL, 'http://v.juhe.cn/toutiao/index?key='.$apikey.'&word='.$word);

        /*'http://api.tianapi.com/keji/?key='.$apikey.'&num=10&word='.$word*/ 
        //将curl_exec()获取的信息 以文件流的形式,而不是直接输出的形式得到
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

        //curl执行
        $data = curl_exec($curl);

        //关闭curl
        curl_close($curl);

        //处理JSON数据

        $jsonObj = json_decode($data);
		/*echo "<pre>";
		print_r($jsonObj);
		echo "<pre>";
		DIE;*/
        //提取文章信息
        // $newslist = $jsonObj->newslist;
        // var_dump($jsonObj->showapi_res_body->pagebean->contentlist);
//      echo '<pre>';
//      print_r($jsonObj->showapi_res_body->pagebean->contentlist);
//         var_dump($data);
//      die();
        $this->assign('list', $jsonObj->showapi_res_body->pagebean->contentlist);
		return $this->fetch();
	}
}
